package com.huidao.dao.buyer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huidao.entity.Goods;
import com.huidao.entity.Order;
import com.huidao.util.JDBCUtil;

public class OrderDaoImp implements OrderDao {
	private static Connection conn = null;
	private static PreparedStatement ps = null;
	private static ResultSet rs = null;

	@Override
	public boolean addOrder(Order order) {
		String sql = "INSERT INTO ORDER1 (GOODS_ID,GOODS_NUM,USER_ID) VALUES(?,?,?)";
		try {
			conn = JDBCUtil.getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setLong(1, order.getGoods().getGid());
			ps.setInt(2, order.getGoods_num());
			ps.setLong(3, order.getUser().getUserId());
			int i = ps.executeUpdate();
			if (i > 0) {
				String sql1 = "UPDATE GOODS SET GOODS_ACCOUNT=? WHERE GOODS_ID=?";
				ps = conn.prepareStatement(sql1);
				ps.setLong(1, order.getGoods().getgAccount()-order.getGoods_num());
				ps.setLong(2, order.getGoods().getGid());
				int j = ps.executeUpdate();
				if (j>0) {
					conn.commit();
				return true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean updateOrder(Order order) {
		String sql = "UPDATE ORDER1 SET ORDER_STATE = ? WHERE ORDER_ID =?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, order.getOrder_state()+1);
			ps.setLong(2, order.getOrder_id());
			int i = ps.executeUpdate();
			if (i > 0) {
				
				return true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	public List<Order> getListOrder( int currenpage,int pagesize) {
		List<Order> list = new ArrayList<Order>();
		String sql = "SELECT * FROM  ORDER1 A  LEFT JOIN GOODS B ON A.GOODS_ID = B.GOODS_ID  LIMIT ?,?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (currenpage - 1) * pagesize);
			ps.setInt(2, pagesize);
			rs = ps.executeQuery();
			while (rs.next()) {
				Order order = new Order();
				Goods goods = new Goods();
				goods.setgPrice(rs.getDouble("GOODS_PRICE"));
				goods.setgName(rs.getString("GOODS_NAME"));
				order.setGoods(goods);
				order.setGoods_num(rs.getInt("GOODS_NUM"));
				order.setOrder_state(rs.getInt("ORDER_STATE"));
				order.setOrder_id(rs.getLong("ORDER_ID"));
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	@Override
	public int total(int currenpage,int pagesize) {
		String sql = "SELECT COUNT(*) FROM ORDER1 WHERE 1=1";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return 0;
	}
	
}
